﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_StudentPages_Students : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Role"] != null && "Student".Equals(Session["Role"]) == false)
        {
            Response.Redirect("~/Pages/HomePage/Home.aspx");
        }
    }
    private void getAllCompetitions()
    {
        //ManagerBUS m = new ManagerBUS();
        StudentBUS student = new StudentBUS();
        //DataSet ds = m.getAllCompetition();
        DataSet ds = student.getGoingCompetition();
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
        }
        else
        {
            grvCompetition.DataSource = ds.Tables[0];
            grvCompetition.DataBind();
        }
    }
    private void getAllDesign()
    {
        StudentBUS st = new StudentBUS();
        int id = Convert.ToInt16(Session["Id"]);
        DataSet ds = st.getAllDesign(id);
        if (ds.Tables[0].Rows.Count > 0)
        {
            grvDesign.DataSource = ds.Tables[0];
            grvDesign.DataBind();
        }
        else
        {
            Response.Redirect("StudentsHome.aspx");
        }
    }

    protected void grvCompetition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='StudentUpload.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void btnSeachCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlockStudent('StudentViewCompetition');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachCompetition.Text;
        if ("".Equals(name))
        {
            getAllCompetitions();
        }
        else
        {
            grvCompetition.DataSource = m.searchCompetition(name).Tables[0];
            grvCompetition.DataBind();
        }
    }
    protected void btnDisplayAllCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlockStudent('StudentViewCompetition');", true);
        getAllCompetitions();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlockStudent('StudentViewDesign');", true);
        getAllDesign();
    }
    protected void grvDesign_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='viewThisDesign.aspx?dId=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlockStudent('StudentViewDesign');", true);
        StudentBUS st = new StudentBUS();
        string name = TextBox1.Text;
        if ("".Equals(name))
        {
            getAllDesign();
        }
        else
        {
            int id = Convert.ToInt16(Session["Id"]);
            grvDesign.DataSource = st.getDesignByComp(id,name).Tables[0];
            grvDesign.DataBind();
        }

    }
    protected void btnSearchDesign_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlockStudent('StudentViewDesign');", true);
        StudentBUS st = new StudentBUS();
        string name = txtSearchDesign.Text;
        if ("".Equals(name))
        {
            getAllDesign();
        }
        else
        {
            int id = Convert.ToInt16(Session["Id"]);
            grvDesign.DataSource = st.getDesignByName(id, name).Tables[0];
            grvDesign.DataBind();
        }
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        string currentPass = txtCurrentPass.Text;
        StudentBUS student = new StudentBUS();
        DataTable dt = student.studentLogin(Session["userName"].ToString(), currentPass).Tables[0];
        if (!((dt.Rows.Count) > 0))
        {
            lblWarningChangePass.Text = "CurrentPass not correct";
            return;
        }
        string newPass = txtNewPass.Text;
        string confirmPass = txtConfirmPass.Text;
        if (!(newPass.Equals(confirmPass)))
        {
            lblWarningChangePass.Text = "ConfirmPass not correct";
            return;
        }
        Student s = new Student();
        s.Id =Convert.ToInt32(dt.Rows[0][0].ToString());
        s.Username = Session["userName"].ToString();
        s.Password = newPass;
        s.Fullname = dt.Rows[0][3].ToString();
        s.Address = dt.Rows[0][4].ToString();
        s.Email = dt.Rows[0][5].ToString();
        s.PhoneNumber = dt.Rows[0][6].ToString();
        s.Major = dt.Rows[0][7].ToString();
        s._class1 = dt.Rows[0][8].ToString();
        StaffBUS staff = new StaffBUS();
        if (staff.updateStudent(s))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningChangePass.Text = "Failed";
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        StaffBUS staff = new StaffBUS();
        DataTable dt = staff.searchStudent(Session["userName"].ToString()).Tables[0];
        Student s = new Student();
        s.Id = Convert.ToInt32(dt.Rows[0][0].ToString());
        s.Username = Session["userName"].ToString();
        s.Password = dt.Rows[0][2].ToString();
        s.Fullname = txtFullNameUpdate.Text;
        s.Address = txtAddressUpdate.Text;
        s.Email = txtEmailUpdate.Text;
        s.PhoneNumber = txtPhoneUpdate.Text;
        s.Major = txtMajorUpdate.Text;
        s._class1 = txtClassUpdate.Text;
        if (staff.updateStudent(s))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningUpdateInfo.Text = "Failed";
        }
    }
}